package gov.kqqzyyy.monkey.orderpad.mytools;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import gov.kqqzyyy.monkey.orderpad.dialogFragment.ProgressDialogFragment;
import gov.kqqzyyy.monkey.orderpad.object.StoredDataJson;
import gov.kqqzyyy.monkey.orderpad.object.post_param.StoredOrder;


/**
 * Created by Monkey on 2017/10/23.
 * 自定义基Activity
 */
public class MyBaseActivity extends AppCompatActivity {

    private ProgressDialogFragment progressDialogFragment;
    private MyApplication myApplication;
    private SharedPreferences mySharedPreferences;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myApplication = (MyApplication) getApplication();
        mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    }

    public MyApplication getMyApplication() {
        return myApplication;
    }

    public SharedPreferences getMySharedPreferences() {
        return mySharedPreferences;
    }

    public void cancelProgressDialog() {
        if (progressDialogFragment != null && progressDialogFragment.getDialog() != null) {
            progressDialogFragment.getDialog().dismiss();
        }
    }

    public void showProgressDialog(String message) {
        cancelProgressDialog();
        progressDialogFragment = new ProgressDialogFragment();
        progressDialogFragment.setMessage(message);
        progressDialogFragment.show(getFragmentManager(), "downLoading");
    }

    public void addSaveOrder(StoredOrder saveOrder) {
        String storedDataJson = mySharedPreferences.getString("storeddata", "");
        List<StoredOrder> list;
        String dateNow = DateTimeUtil.getNewServerDate(myApplication.getDeltaTime());
        Gson gson = new Gson();
        StoredDataJson storedData;
        if (!Objects.equals(storedDataJson, "")) {
            storedData = gson.fromJson(storedDataJson, new TypeToken<StoredDataJson>() {
            }.getType());
            String storedDate = Objects.requireNonNull(storedData).getSotredDate();
            if (!storedDate.equals(dateNow)) {
                storedData.setSotredDate(dateNow);
                list = new ArrayList<>();
            } else {
                list = gson.fromJson(storedData.getStoredJson(), new TypeToken<List<StoredOrder>>() {
                }.getType());
                if (list == null) {
                    list = new ArrayList<>();
                }
            }
        } else {
            storedData = new StoredDataJson();
            storedData.setSotredDate(dateNow);
            list = new ArrayList<>();
        }
        list.add(saveOrder);
        String listJson = gson.toJson(list);
        storedData.setStoredJson(listJson);
        String resultJson = gson.toJson(storedData);
        mySharedPreferences.edit().putString("storeddata", resultJson).apply();
    }

    public void clearChoosePatientInfo() {

    }

    protected void showToast(String message) {
        Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
    }
}
